package com.etc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.etc.dao.UserDao;
import com.etc.entity.User;
import com.etc.util.PageData;
import com.etc.util.UserRowMapper;

@Repository(value = "userDao")
public class UserDaoImpl implements UserDao {
	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 查询所有用户
	 */
	@Override
	public List<User> getUserAll(int page, int limit) {
		// TODO Auto-generated method stub
		int start = (page - 1) * limit;
		String sql = "select * from user " + " limit " + start + "," + limit;
		List<User> list = jdbcTemplate.query(sql, new UserRowMapper());
		return list;
	}

	@Override
	public int selectCount() {
		String sql = "select count(*) from user";
		int n = jdbcTemplate.queryForObject(sql, Integer.class);
		return n;
	}

	@Override
	/**
	 * 通过账户密码查询用户的方法
	 */
	public User getUser(String userName, String userPwd) {
		String sql = "select * from user where userName=? and userPwd=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { userName, userPwd }, new UserRowMapper());
	}

	@Override
	/**
	 * 分页加模糊查询用户列表的信息
	 */
	public PageData<User> getUserByPage(int page, int pageSize, String content) {
		// and userLevel>0
		String sql = "select * from user where userName like ? ";
		// 统计记录数量
		String selSql = "select count(1) from (" + sql + ") t";
		// 调用getFirst 得到记录数
		Integer count = jdbcTemplate.queryForObject(selSql, Integer.class, "%" + content + "%");
		// 得到起始位置
		int start = (page - 1) * pageSize;
		// 拼接实际的分页的sql语句
		selSql = sql + " limit " + start + "," + pageSize;
		// 这里用的是selsql
		List<User> list = jdbcTemplate.query(selSql, new Object[] { "%" + content + "%" }, new UserRowMapper());
		// 构造一个PageData
		PageData<User> data = new PageData<User>(list, count, pageSize, page);
		return data;

	}

	/**
	 * 保存用户信息的方法
	 */
	@Override
	public int saveUser(User user) {
		String sql = "insert into user(userName,userPwd,userSex,userTel,userAddress,userLevel,userState) values(?,?,?,?,?,?,?)";
		int n = jdbcTemplate.update(sql, user.getUserName(), user.getUserPwd(), user.getUserSex(), user.getUserTel(),
				user.getUserAddress(), user.getUserLevel(), user.getUserState());
		return n;
	}

	/**
	 * 通过id获取用户信息的方法
	 */
	@Override
	public User getUserById(int userId) {
		String sql = "select * from user where userId = ?";
		return jdbcTemplate.queryForObject(sql, new Object[] { userId }, new UserRowMapper());
	}

	/**
	 * 修改用户信息的方法
	 */
	@Override
	public int updateUser(User user) {
		String sql = "update user set userName=?,userSex=?,userTel=?,userAddress=?,userLevel=?,userState=?  where userId=?";
		int n = jdbcTemplate.update(sql, user.getUserName(), user.getUserSex(), user.getUserTel(),
				user.getUserAddress(), user.getUserLevel(), user.getUserState(), user.getUserId());
		return n;
	}

	/**
	 * 根据id删除用户的方法
	 */
	@Override
	public int delete(int userId) {
		String sql = "delete from  user where userId=?";
		int n = jdbcTemplate.update(sql, userId);
		return n;
	}

	/**
	 * 根据用户名查询用户的方法
	 */
	@Override
	public User finduser(String userName) {
		// TODO Auto-generated method stub
		String sql = "select * from user where userName=?";
		return jdbcTemplate.queryForObject(sql, new Object[] { userName }, new UserRowMapper());
	}

	@Override
	public List<User> getUserAll() {
		// TODO Auto-generated method stub
		String sql = "select * from user";
		List<User> list = jdbcTemplate.query(sql, new UserRowMapper());
		return list;
	}

}
